package com.xayy.health.ds.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.BeanUtil;
import cn.kawins.base.utils.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xayy.health.ds.model.DsTaskChildInfo;
import com.xayy.health.ds.model.DsTaskInfo;
import com.xayy.health.ds.service.DsTaskChildInfoService;
import com.xayy.health.ds.service.DsTaskInfoService;
import com.xayy.health.mgt.model.vo.PreprocessParamVo;
import com.xtck.ha.common.core.domain.R;
import com.xtck.ha.common.core.web.service.dsimpl.BaseServiceImpl;
import com.xtck.ha.common.core.config.DsServerConfig;
import com.xtck.ha.common.core.utils.DomainUtils;
import com.xayy.health.ds.model.DsTaskParamInfo;
import com.xayy.health.ds.service.DsTaskParamInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: DsTaskParamInfoService实现类
 * @Auther:liuyuan
 * @Date:2025-07-08
 */
@Slf4j
@Service
public class DsTaskParamInfoServiceImpl extends BaseServiceImpl<DsTaskParamInfo> implements DsTaskParamInfoService {
  /**
  *
  * 冗余代码，为自定义开发DS-Server接口服务
  *
  */
   private static String TABLE_NAME = "TB_TASK_PARAM_INFO";
   private String getUrl(){
      return getDsServerConfig().getUrl();
   }
   @Autowired
    DsTaskInfoService dsTaskInfoService;
   @Autowired
    DsTaskChildInfoService dsTaskChildInfoService;

    @Override
    public CheckResult<Object> preprocessParam(PreprocessParamVo preprocessParamVo) {
        StrBuilder msg = new StrBuilder();
        DsTaskParamInfo taskParamInfo = new DsTaskParamInfo();
        List<DsTaskParamInfo> dsTaskParamInfoList = selectTList(DsTaskParamInfo.builder().satelliteCode(preprocessParamVo.getSatelliteCode())
                .paramCode(preprocessParamVo.getParamCode()).status(1).build());
        if (null != dsTaskParamInfoList && dsTaskParamInfoList.size()>0){
            taskParamInfo = dsTaskParamInfoList.get(0);
        }
        if (BeanUtil.isNotEmpty(taskParamInfo)) {
            DsTaskInfo taskInfo = dsTaskInfoService.selectTById(taskParamInfo.getTaskId());
            Date startTime = taskInfo.getStartTime();
            Date endTime = taskInfo.getEndTime();
            String startTimeStr = DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss");
            String endTimeStr = DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss");
            msg.append("参数：").append(preprocessParamVo.getParamCode()).append("在数据预处理任务：").append(taskInfo.getName())
                    .append("；时间段：").append(startTimeStr).append("--").append(endTimeStr).append("；中进行了预处理");
            if (StrUtil.isNotBlank(taskParamInfo.getTyName())) {
                msg.append("，使用剔野算法为：");
                msg.append(taskParamInfo.getTyName());
            }
            if (StrUtil.isNotBlank(taskParamInfo.getTcName())) {
                msg.append("，使用填充算法为：");
                msg.append(taskParamInfo.getTcName());
            }
            if (StrUtil.isNotBlank(taskParamInfo.getPhName())) {
                msg.append("，使用平滑算法为：");
                msg.append(taskParamInfo.getPhName());
            }
            List<DsTaskChildInfo> childInfo = dsTaskChildInfoService.selectTList(DsTaskChildInfo.builder().taskId(taskInfo.getId()).status(4).build());
            if(CollUtil.isNotEmpty(childInfo)){
                msg.append("【以下时间段被清除");
                for (DsTaskChildInfo taskChildInfo : childInfo) {
                    if(taskChildInfo.getStatus()==4){
                        msg.append(DateUtil.format(taskChildInfo.getStartTime(), "yyyy-MM-dd HH:mm:ss")).append("到").append(DateUtil.format(taskChildInfo.getEndTime(), "yyyy-MM-dd HH:mm:ss")).append(",");
                    }
                }
                msg.append("】");
            }
        }else {
            msg.append("参数：").append(preprocessParamVo.getParamCode()).append("未进行过数据预处理");
        }

        return CheckResult.success(msg.toString());
    }

    @Override
    /**
     * 根据任务id批量删除参数信息
     *
     * @param taskId 任务id
     */
    public void deleteBatchParams(List<Long> taskId){
        String sql = "delete from HEALTH.tb_task_param_info where tb_task_param_info.task_id in\n (";
        for (long id : taskId){
            sql += id+",";
        }
        if (sql.endsWith(",")) {
            sql = sql.substring(0, sql.length() - 1);
        }
        sql += ");";
        selectBySql(sql);
    }
}
